//package nongchang.kuaijiecaifu.com.ncapp_android.ui;
//
//import android.content.Context;
//import android.content.DialogInterface;
//import android.database.DataSetObserver;
//import android.support.v4.widget.SwipeRefreshLayout;
//import android.support.v7.app.AlertDialog;
//import android.support.v7.app.AppCompatActivity;
//import android.os.Bundle;
//import android.support.v7.widget.LinearLayoutManager;
//import android.support.v7.widget.RecyclerView;
//import android.text.Editable;
//import android.text.TextWatcher;
//import android.util.Log;
//import android.view.KeyEvent;
//import android.view.MotionEvent;
//import android.view.View;
//import android.view.inputmethod.EditorInfo;
//import android.view.inputmethod.InputMethodManager;
//import android.widget.EditText;
//import android.widget.LinearLayout;
//import android.widget.TextView;
//import android.widget.Toast;
//
//import org.w3c.dom.Text;
//
//import java.util.ArrayList;
//
//import butterknife.BindView;
//import butterknife.ButterKnife;
//import butterknife.OnClick;
//import ezy.lite.util.UI;
//import nongchang.kuaijiecaifu.com.ncapp_android.R;
//import nongchang.kuaijiecaifu.com.ncapp_android.common.activity.BaseActivity;
//import nongchang.kuaijiecaifu.com.ncapp_android.retrofit.data.CartManager;
//import nongchang.kuaijiecaifu.com.ncapp_android.retrofit.good.Cart;
//import nongchang.kuaijiecaifu.com.ncapp_android.ui.adapter.CartAdapter;
//import nongchang.kuaijiecaifu.com.ncapp_android.ui.widget.LoadingLayout;
//
//public class CartActivity extends BaseActivity {
//    @BindView(R.id.ll_layout)
//    LinearLayout linearLayout;
//    @BindView(R.id.refresh)
//    SwipeRefreshLayout mRefresh;
//    @BindView(R.id.list)
//    RecyclerView mListView;
//    @BindView(R.id.loading)
//    LoadingLayout mLoading;
//    @BindView(R.id.txt_total_money)
//    TextView txtTotal;
//    private CartAdapter cartAdapter;
//    private AlertDialog.Builder builder;
//    private int screenHeight = 0;
//    //软件盘弹起后所占高度阀值
//    private int keyHeight = 0;
//    private RecyclerView.AdapterDataObserver sumObserver = new RecyclerView.AdapterDataObserver() {
//
//        @Override
//        public void onChanged() {
//            super.onChanged();
//            //执行相应的操作
//            float sum = 0;
//            for (int i = 0; i < CartManager.cartList.size(); i++) {
//                sum += CartManager.cartList.get(i).total_money;
//            }
//            txtTotal.setText("总金额:" + sum);
//        }
//    };
//
//    @Override
//    protected void onCreate(Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_cart);
//        setupToolbarWithHome(R.id.toolbar);
//        ButterKnife.bind(this);
//        screenHeight = this.getWindowManager().getDefaultDisplay().getHeight();
//        //阀值设置为屏幕高度的1/3
//        keyHeight = screenHeight/3;
//        mListView.setLayoutManager(new LinearLayoutManager(this));
//        cartAdapter = new CartAdapter(this);
//        mListView.setAdapter(cartAdapter);
//        cartAdapter.registerAdapterDataObserver(sumObserver);
//        setListener();
//
//        mRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
//            @Override
//            public void onRefresh() {
//                mRefresh.setRefreshing(true);
//            }
//        });
//        load();
//    }
//
//
//    private void load() {
//        CartManager.cartList = new ArrayList<>();
//        Cart product = null;
//        for (int i = 1; i <= 10; i++) {
//            product = new Cart();
//            product.one_price = i;
//            product.num = 1;
//            product.title = "商品" + i;
//            product.image = "http://banbao.chazidian.com/uploadfile/2016-01-25/s145368924044608.jpg";
//            CartManager.cartList.add(product);
//            product.total_money = i;
//        }
//        mLoading.showContent();
//        cartAdapter.update(CartManager.cartList);
//    }
//
//
//    private void setListener() {
//        cartAdapter.setOnAddNum(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Object tag = v.getTag();
//                if (tag != null && tag instanceof Integer) { //解决问题：如何知道你点击的按钮是哪一个列表项中的，通过Tag的position
//                    int position = (Integer) tag;
//                    int num = CartManager.cartList.get(position).num;
//                    num++;
//                    CartManager.cartList.get(position).num = num; //修改集合中商品数量
//                    CartManager.cartList.get(position).total_money = (CartManager.cartList.get(position).one_price * num); //修改集合中该商品总价 数量*单价
//                    //解决问题：点击某个按钮的时候，如果列表项所需的数据改变了，如何更新UI
//                    cartAdapter.update(CartManager.cartList);
//                }
//            }
//        });
//        cartAdapter.setOnSubNum(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Object tag = v.getTag();
//                if (tag != null && tag instanceof Integer) { //解决问题：如何知道你点击的按钮是哪一个列表项中的，通过Tag的position
//                    int position = (Integer) tag;
//                    int num = CartManager.cartList.get(position).num;
//                    if (num <= 1) {
//                        return;
//                    }
//                    num--;
//                    CartManager.cartList.get(position).num = num; //修改集合中商品数量
//                    CartManager.cartList.get(position).total_money = (CartManager.cartList.get(position).one_price * num); //修改集合中该商品总价 数量*单价
//                    //解决问题：点击某个按钮的时候，如果列表项所需的数据改变了，如何更新UI
//                    cartAdapter.update(CartManager.cartList);
//                }
//            }
//        });
//
//        cartAdapter.setOnDeleteNum(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                builder = new AlertDialog.Builder(CartActivity.this);
//                builder.setMessage("您确定要删除吗？");
//                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
//                    @Override
//                    public void onClick(DialogInterface dialog, int which) {
//                        Object tag = v.getTag();
//                        if (tag != null && tag instanceof Integer) { //解决问题：如何知道你点击的按钮是哪一个列表项中的，通过Tag的position
//                            int position = (Integer) tag;
//                            CartManager.cartList.remove(position);
//                            cartAdapter.update(CartManager.cartList);
//                        }
//                        builder = null;
//                    }
//                });
//                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
//                    @Override
//                    public void onClick(DialogInterface dialog, int which) {
//                        builder = null;
//                    }
//                });
//                //    显示出该对话框
//                builder.show();
//            }
//        });
//        cartAdapter.setOnTotalNum(new View.OnFocusChangeListener() {
//            @Override
//            public void onFocusChange(View v, boolean hasFocus) {
//                if (hasFocus) {
//                    Object tag = v.getTag();
//                    ((EditText) v).addTextChangedListener(new TextWatcher() {
//                        @Override
//                        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//
//                        }
//
//                        @Override
//                        public void onTextChanged(CharSequence s, int start, int before, int count) {
//
//                        }
//
//                        @Override
//                        public void afterTextChanged(Editable s) {
//                            if (mListView.isComputingLayout()) {
//                                return;
//                            }
////                                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
////                                boolean isOpen = imm.isActive();
//                            if (s.toString().equals("")) {
//                                ((EditText) v).setText("1");
//                                if (tag != null && tag instanceof Integer) {
//                                    int position = (Integer) tag;
//                                    CartManager.cartList.get(position).num = 1;
//                                    CartManager.cartList.get(position).total_money = (CartManager.cartList.get(position).one_price * 1);
//                                }
//                            } else {
//                                if (tag != null && tag instanceof Integer) {
//                                    int position = (Integer) tag;
//                                    CartManager.cartList.get(position).num = Integer.parseInt(((EditText) v).getText().toString());
//                                    CartManager.cartList.get(position).total_money = (CartManager.cartList.get(position).one_price * CartManager.cartList.get(position).num);
//                                }
//                            }
//                        }
//                    });
//                }else{
//                    if (mListView.isComputingLayout()) {
//                        return;
//                    }
//                    cartAdapter.update(CartManager.cartList);
//                }
//            }
//        }, new TextView.OnEditorActionListener() {
//            @Override
//            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
//                if (actionId == EditorInfo.IME_ACTION_DONE
//                        || (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
//                    //do something;
//                    mListView.requestFocus();
//                    InputMethodManager imm = (InputMethodManager)
//                            getSystemService(Context.INPUT_METHOD_SERVICE);
//                    imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
//                    return true;
//                }
//                return false;
//            }
//        });
//    }
//
//    @Override
//    protected void onResume() {
//        super.onResume();
//        linearLayout.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
//            @Override
//            public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
//                if(oldBottom != 0 && bottom != 0 &&(oldBottom - bottom > keyHeight)){
//
////                    Toast.makeText(CartActivity.this, "监听到软键盘弹起...", Toast.LENGTH_SHORT).show();
//
//                }else if(oldBottom != 0 && bottom != 0 &&(bottom - oldBottom > keyHeight)){
////                    Toast.makeText(CartActivity.this, "监听到软件盘关闭...", Toast.LENGTH_SHORT).show();
//                    mListView.post(new Runnable() {
//                        @Override
//                        public void run() {
//                            mListView.requestFocus();
//                        }
//                    });
//                }
//            }
//        });
//    }
//
//    @OnClick({R.id.btn_jiesuan})
//    void OnClick(View view) {
//        switch (view.getId()) {
//            case R.id.btn_jiesuan:
//                break;
//        }
//    }
//
//}
